package org.zjvis.graph.analysis.service.util;

import javax.annotation.Nonnull;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.util.Objects;

public class FileUtil {
    public static String readJsonFile(String resourceFilePath) {
        try {
            InputStream inputStream = FileUtil.class.getClassLoader().getResourceAsStream(resourceFilePath);
            return inputStreamToString(Objects.requireNonNull(inputStream));
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

    @Nonnull
    private static String inputStreamToString(@Nonnull InputStream inputStream) throws IOException {
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int length;
        while (-1 != (length = inputStream.read(buffer))) {
            result.write(buffer, 0, length);
        }
        return result.toString("utf-8");
    }
}
